package mvc.model.dao.impl;

import mvc.common.BaseDao;
import mvc.model.dao.UserDao;
import mvc.model.pojos.UserBean;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private final static BaseDao baseDao = new BaseDao();

    @Override
    public UserBean login(String uname, String upass) {
        String sql = "select * from user where uname=? and upass=?";
        List<UserBean> query = baseDao.query(sql, UserBean.class, uname, upass);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public UserBean queryOne(int uid) {
        String sql = "select * from user where uid=?";
        List<UserBean> query = baseDao.query(sql, UserBean.class, uid);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public UserBean queryOne(String uname) {
        String sql = "select * from user where uname=?";
        List<UserBean> query = baseDao.query(sql, UserBean.class, uname);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public List<UserBean> query() {
        String sql = "select * from user";
        return baseDao.query(sql, UserBean.class);
    }

    @Override
    public boolean updateUser(int uid, UserBean userBean) {
        String sql = "UPDATE `user` SET `uname` = ?, `upass` = ?, `aid` = ?, `utime` = ?, `username` = ?, `uemail` = ?, `phone` = ?, `hid` = ? WHERE (`uid` = ?);";
        int update = baseDao.update(sql, userBean.getUname(), userBean.getUpass(), userBean.getAid(), userBean.getUtime(), userBean.getUsername(), userBean.getUemail(), userBean.getPhone(), userBean.getHid(), uid);
        return update > 0;
    }

    @Override
    public boolean addUser(UserBean userBean) {
        String sql = "insert into userBean(uname,upass,phone) values(?,?,?)";
        baseDao.update(sql, userBean.getUname(), userBean.getUpass(), userBean.getPhone());
        String query = "select * from userBean where uname=?";
        List<UserBean> list = baseDao.query(query, UserBean.class, userBean.getUname());
        return list.isEmpty();
    }

}
